package com.example.work4

import android.net.NetworkInfo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.work4.adapter.MessageListAdapter
import com.example.work4.http.MessageRetrofitImp
import com.example.work4.po.Message
import com.example.work4.po.SSOInfo
import com.example.work4.view.EditMessageDialog
import com.example.work4.view.StatusBar
import com.farsunset.cim.sdk.android.CIMEventListener
import com.farsunset.cim.sdk.android.CIMListenerManager
import com.farsunset.cim.sdk.android.model.ReplyBody
import com.farsunset.cim.sdk.android.model.SentBody


class ChatActivity : AppCompatActivity(),View.OnClickListener, CIMEventListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_chat)
        StatusBar.setStatusBarAsBlue(window)
        init()
    }

    var id = 0;
    var input :TextView? = null
    val adapter = MessageListAdapter()
    val messageRetrofitImp = MessageRetrofitImp(this)
    var recyclerView:RecyclerView? = null
    var dialog = EditMessageDialog(this,this)

    fun init(){
        val text = findViewById<TextView>(R.id.nickname)
        val sendMessage = findViewById<Button>(R.id.sendMessage)
        sendMessage.setOnClickListener(this)
        id = intent.getIntExtra("id",-1)
        input = findViewById(R.id.messageInput)
        input!!.setOnClickListener(this)
        val avatar = intent.getStringExtra("avatar")
        val nickname = intent.getStringExtra("nickname")
        text.text = nickname
        recyclerView = findViewById<RecyclerView>(R.id.messageRecyclerView);
        val linearLayoutManager = LinearLayoutManager(this);
        linearLayoutManager.stackFromEnd = true;
        recyclerView!!.layoutManager = linearLayoutManager
        adapter.avatar = avatar!!
        messageRetrofitImp.get(recyclerView!!,adapter,id)
        CIMListenerManager.registerMessageListener(this);
    }


    override fun onClick(v: View?) {
        when(v!!.id)
        {
            R.id.sendMessage ->{
                if(input!!.text.toString() == ""){
                    dialog.show()
                }
                else {
                    val mess = Message()
                    mess.sender = SSOInfo.id!!
                    mess.receiver = id
                    mess.action = "1"
                    mess.content = dialog.input!!.text.toString()
                    messageRetrofitImp.send(recyclerView!!, adapter, mess)
                }
            }
            R.id.messageInput -> {
                dialog.show()
            }
            R.id.sendComment ->{
                val mess = Message()
                mess.sender = SSOInfo.id!!
                mess.receiver = id
                mess.action = "1"
                mess.content = dialog.input!!.text.toString()
                messageRetrofitImp.send(recyclerView!!, adapter, mess)
            }
        }
    }

    override fun onMessageReceived(message: com.farsunset.cim.sdk.android.model.Message?) {
       val m = Message.toMessage(message)
        adapter.data.add(m)
        adapter.update()
        recyclerView!!.scrollToPosition(adapter.itemCount -1);
    }

    override fun onReplyReceived(body: ReplyBody?) {

    }

    override fun onSendFinished(body: SentBody?) {

    }

    override fun onNetworkChanged(info: NetworkInfo?) {

    }

    override fun onConnectFinished(hasAutoBind: Boolean) {

    }

    override fun onConnectionClosed() {

    }

    override fun onConnectFailed() {

    }

    override fun getEventDispatchOrder(): Int {
       return 100
    }

    override fun onDestroy() {
        DataUtil.messageHandler!!.sendEmptyMessage(DataUtil.UPDATE_MESSAGE_LIST)
        super.onDestroy()
    }

}